package com.est.dase;

import android.app.Activity;
import android.content.res.Resources;
import android.database.Cursor;
import android.location.Location;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts.Data;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.est.dase.sms.SMS;
import com.est.dase.widget.Status;
/**
*
* @author Fernando Geraldes, Jaime Santos, Jorge Silva
*
*/
public class MessageDisplay extends Activity {

	//	private ArrayList<String> listName = new ArrayList<String>();
	//	private ArrayList<String> listNumber = new ArrayList<String>();
	private SMS smsSender;
	private String number;
	Location location;
	//LocationManager locationManager;
	MyLocation myLocation = new MyLocation();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_display);
		
		smsSender = new SMS();

		Bundle bundle = getIntent().getExtras();
		number = bundle.getString("number");

		Button yes = (Button) findViewById(R.id.button_yes);

		Button no = (Button) findViewById(R.id.button_no);

		TextView text = (TextView) findViewById(R.id.text_message_display);

		String n = number.replaceAll("\\+351", "");
		Log.v("number",n);
		String from = getContact(n);
		Log.v("FROM",from);
		if(from == null || from.equals("")){
			from = n;
		}
		
		if(bundle.getString("action").equals(Params.way)){
		
			//String textFrom = "Do you want to share your location to "+from+" ?";
			final Resources res = getResources();
			String textFrom = res.getString(R.string.messageDisplay_from) + " " + from+"?";
	
			text.setText(textFrom);
	
			location = myLocation.getLocation(this);
			
			yes.setOnClickListener(new OnClickListener() {
	
				public void onClick(View v) {

					if (location != null){
						smsSender.sendSMS(number, Params.position+" "+location.getLatitude()+" "+location.getLongitude()+"", MessageDisplay.this);
					}else{
						smsSender.sendSMS(number, res.getString(R.string.messageDisplay_send), MessageDisplay.this);
					}
					finish();
				}
			});
		}else if(bundle.getString("action").equals(Params.wys)){

			final Resources res = getResources();
			String textFrom = res.getString(R.string.messageDisplay_shareStatus) + " " + from+"?";
	
			text.setText(textFrom);
			
			yes.setOnClickListener(new OnClickListener() {
	
				public void onClick(View v) {
					
					if(Status.getInstance(MessageDisplay.this).getStatus() == 4)
						smsSender.sendSMS(number, Params.wysStatus+" "+Status.getInstance(MessageDisplay.this).getOutro()+"", MessageDisplay.this);
					else
						smsSender.sendSMS(number, Params.wysStatus+" "+Status.getInstance(MessageDisplay.this).getCurrentStatus()+"", MessageDisplay.this);
					
					finish();
				}
			});			
		}
		
		
		no.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});

	}
	
	public String getContact(String number){
		final String[] projection = new String[] {
				Phone.DISPLAY_NAME
		};
		final Cursor phone = managedQuery(
				Phone.CONTENT_URI,
				projection,
				Data.DATA1 + "=?",
				new String[]{number},
				null);
		if(phone.moveToFirst()) {
			int contactNumberColumnIndex = phone.getColumnIndex(Phone.DISPLAY_NAME);
			number = phone.getString(contactNumberColumnIndex);
		}
		phone.close();

		return number;
	}

}
